//
//  ACLiveTypeSelectedViewController.m
//  ACLiteAVUISDK
//
//  Created by 冯琰琰 on 2021/3/29.
//

#import "ACLiveTypeSelectedViewController.h"
#import "ACLiveTypeSelectedCell.h"


@interface ACLiveTypeSelectedViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableview;
@property(nonatomic,strong)NSMutableArray<NSDictionary *> *array;
@property(nonatomic,assign)NSInteger selectedIndex;
@end


@implementation ACLiveTypeSelectedViewController

-(UITableView *)tableview{
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.estimatedRowHeight = 68;
        _tableview.backgroundColor = [UIColor clearColor];
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        NSBundle *curB = [NSBundle BundleWithBundleName:ACLiteAVUISDK targetClass:[self class] ];
        [_tableview registerNib:[UINib nibWithNibName:@"ACLiveTypeSelectedCell" bundle:curB] forCellReuseIdentifier:@"ACLiveTypeSelectedCell"];
    }
    return _tableview;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    self.array = [[NSMutableArray alloc] initWithObjects:@{@"title":@"互动直播",@"isSelected":@"0"},@{@"title":@"分组对抗",@"isSelected":@"0"},@{@"title":@"培训",@"isSelected":@"0"},@{@"title":@"路演",@"isSelected":@"1"}, nil];
    
    self.subject = [RACSubject subject];
    
    self.selectedIndex = self.array.count - 1;
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(68 * self.array.count);
        make.top.mas_equalTo(180);
    }];
    
    UIButton *startLive = [UIButton buttonWithType:UIButtonTypeCustom];
    startLive.layer.masksToBounds = YES;
    startLive.layer.cornerRadius = 24;
    [startLive setTitle:@"开启直播" forState:UIControlStateNormal];
    [startLive setBackgroundColor:UIColorFromRGB(0xFF6048)];
    [startLive.titleLabel setFont:[UIFont systemFontOfSize:16 weight:UIFontWeightMedium]];
    [startLive setTitleColor:UIColorFromRGB(0xffffff) forState:UIControlStateNormal];
    [self.view addSubview:startLive];
    [startLive mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(48);
        make.left.equalTo(self.view.mas_left).offset(48);
        make.right.equalTo(self.view.mas_right).offset(-48);
        make.top.equalTo(self.tableview.mas_bottom).offset(30);
    }];
    @weakify(self);
    [[startLive rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self.subject sendNext:@(self.selectedIndex)];
        [self.subject sendCompleted];
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageWithName:@"icon_live_type_close" bundle:ACLiteAVUISDK targetClass:[self class]] forState:UIControlStateNormal];
    [self.view addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.size.mas_equalTo(CGSizeMake(46, 46));
        make.top.equalTo(startLive.mas_bottom).offset(40);
    }];
    [[closeBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        @strongify(self)
        [self dismissViewControllerAnimated:YES completion:nil];
    }];
}
#pragma mark - UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ACLiveTypeSelectedCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ACLiveTypeSelectedCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[ACLiveTypeSelectedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ACLiveTypeSelectedCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.array[indexPath.row];
    return  cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dic = self.array[indexPath.row];
    NSString *value = [dic valueForKey:@"isSelected"];
    NSMutableDictionary *newDic = [dic mutableCopy];
    if (indexPath.row != self.selectedIndex && self.selectedIndex != -1) {
        NSMutableDictionary *selectedDic = [self.array[self.selectedIndex] mutableCopy];
        [selectedDic setValue:@"0" forKey:@"isSelected"];
        [self.array replaceObjectAtIndex:self.selectedIndex withObject:selectedDic];
    }
    if ([value intValue] == 1) {
        [newDic setValue:@"0" forKey:@"isSelected"];
        self.selectedIndex = -1;
    }else{
        [newDic setValue:@"1" forKey:@"isSelected"];
        self.selectedIndex = indexPath.row;
    }
    [self.array replaceObjectAtIndex:indexPath.row withObject:newDic];
    [self.tableview reloadData];
}

@end
